package Interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.vma.tools.AppException;
import com.vma.tools.ReturnsUtil;


/**
 * Created by chenzui on 2017/5/24.
 */
public class ExceptionInterceptor implements Interceptor {
    public void intercept(Invocation invocation) {
        try{
            invocation.invoke();
        }catch (Exception e){
            boolean appEx= e instanceof AppException;
            if(appEx){
                AppException tmp = (AppException) e;
                invocation.getController().renderJson(ReturnsUtil.returnFail(e.getMessage(),((AppException) e).getErrorCode()));
            }else{
                e.printStackTrace();
                invocation.getController().renderJson(ReturnsUtil.returnException(e.getMessage(),e));
            }

        }
    }
}
